package cn.ren.cloud.controller;

import cn.ren.cloud.dto.OrderCourserDto;
import cn.ren.cloud.service.ICourseOrderService;
import cn.ren.cloud.domain.CourseOrder;
import cn.ren.cloud.query.CourseOrderQuery;
import cn.ren.cloud.result.JsonResult;
import cn.ren.cloud.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author 23508
 */
@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {

    public final ICourseOrderService courseOrderService;

    public CourseOrderController(ICourseOrderService courseOrderService) {
        this.courseOrderService = courseOrderService;
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody CourseOrder courseOrder) {
        if (courseOrder.getId() != null) {
            courseOrderService.updateById(courseOrder);
        } else {
            courseOrderService.insert(courseOrder);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        courseOrderService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id") Long id) {
        return JsonResult.success(courseOrderService.selectById(id));
    }

    @RequestMapping(value = "/selectByOrderNo/{orderNo}", method = RequestMethod.GET)
    public CourseOrder selectByOrderNo(@PathVariable("orderNo") String orderNo) {
        CourseOrder courseOrder = courseOrderService.selectByOrderNo(orderNo);
        return courseOrder;
    }
    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(courseOrderService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody CourseOrderQuery query) {
        Page<CourseOrder> page = new Page<CourseOrder>(query.getPage(), query.getRows());
        page = courseOrderService.selectPage(page);
        return JsonResult.success(new PageList<CourseOrder>(page.getTotal(), page.getRecords()));
    }

    /**
     * 下单
     *
     * @param courserDto 封装参数
     * @return JsonResult.success
     */
    @RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
    public JsonResult placeOrder(@RequestBody OrderCourserDto courserDto) {
        return JsonResult.success(courseOrderService.placeOrder(courserDto));
    }
}
